// pages/index/indexDetail.js

const App = getApp();
const navigationBarHeight = (App.statusBarHeight + 44) + 'px'
var config = require('../../utils/config');
var net = require('../../utils/net.js');
var util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    navigationBarTitle: 'Japitta',
    navigationBarHeight,


    showDots:false,
    autoplay:true,
    interval:5000,
    duration:500,
    showShareView:false,
    showTedianView:false,
    imgUrls:[],
    detailModel:{},
    dataArr:[],
    selectModel:{},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    new App.ToastPannel();
    this.shopId = options.shopId;
    if(!util.isBlank(this.shopId)){
      var shopName = options.shopName;
      this.setData({
        navigationBarTitle:shopName
      });

      if (App.globalData.isLogin) {
        this.judgeDingwei();

      } else {
        App.loginCallback = res => {
          this.judgeDingwei();
        }
      }
      return;
    }


    let q = decodeURIComponent(options.q)
    this.shopId = util.getQueryString(q, 'merchantId');

    if (App.globalData.isLogin) {
      this.judgeDingwei();

    } else {
      App.loginCallback = res => {
        this.judgeDingwei();
      }
    }   
  },

  //判断定位
  judgeDingwei:function(){
    if (App.globalData.lat != 0) {
      this.requestDetailData();
    }else{
      this.loadInfo();
    }
  },

  //定位
  loadInfo:function(){
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success: res => {
        console.log(res.latitude, res.longitude);
        App.globalData.lat = res.latitude
        App.globalData.lon = res.longitude
        this.latitude = res.latitude//维度
        this.longitude = res.longitude//经度
        this.requestDetailData();
      },
      fail: res => {
        this.show('定位失败', res);
      }
    }) 
  },

//  请求详情
  requestDetailData:function(){
    var url = config.service.businessDetailsUrl;
    var params = {
      merchantId: this.shopId,
      latitude: App.globalData.lat,
      longitude: App.globalData.lon
    }
    net.doRequest(url, params, 'GET', true, net.Token, res => {
      if (res.code == 0) {
        
        var model = res.data
        if (model.distance > 1000) {
          model.distance = (model.distance / 1000.0).toFixed(1) + 'km'
        } else {
          var num = model.distance;
          model.distance = num.toFixed(1) + 'm';
        }

        wx.setNavigationBarTitle({
          title: model.shopName,
        });
        var timeString = ''
        if (!util.isNull(model.operatedBegin1)){
          console.log('转化时间')
          model.operatedBegin1 = util.zhuanHuaTime(model.operatedBegin1, 'h:m')
          model.operatedEnd1 = util.zhuanHuaTime(model.operatedEnd1, 'h:m')
          timeString = model.operatedBegin1 + '-' + model.operatedEnd1

        }
        if (!util.isNull(model.operatedBegin2)) {
          model.operatedBegin2 = util.zhuanHuaTime(model.operatedBegin2, 'h:m')
          model.operatedEnd2 = util.zhuanHuaTime(model.operatedEnd2, 'h:m')
          timeString = timeString + ' ' + model.operatedBegin2 + '-' + model.operatedEnd2

        }
        if (!util.isNull(model.operatedBegin3)) {
          model.operatedBegin3 = util.zhuanHuaTime(model.operatedBegin3, 'h:m')
          model.operatedEnd3 = util.zhuanHuaTime(model.operatedEnd3, 'h:m')
          timeString = timeString + ' ' + model.operatedBegin3 + '-' + model.operatedEnd3
        } 
        model.timeString = timeString
        var urls = []
        if (!util.isBlank(model.shopImg1)) {
          var url1 = config.service.picUrl + model.shopImg1
          urls.push(url1);
        }
        if (!util.isBlank(model.shopImg2)) {
          var url2 = config.service.picUrl + model.shopImg2
          urls.push(url2);
        }
        if (!util.isBlank(model.shopImg3)) {
          var url3 = config.service.picUrl + model.shopImg3
          urls.push(url3);
        }
        if(urls.length>1){
          this.setData({
            showDots:true
          })
        }

        var teDianList = res.data.wdIscountDtoOutList
        if(teDianList.length > 0){
          for(var i=0; i<teDianList.length; i++){
            var teDian = teDianList[i];
            if (!util.isNull(teDian.limitedAt)){
             
              teDian.limitedAtStr = util.zhuanHuaTime(teDian.limitedAt, 'Y年M月D日')
        
            }

            
          }
        }
    
        this.setData({
          detailModel: model,
          imgUrls:urls,  
          dataArr: res.data.wdIscountDtoOutList
        })
        
      } else {
        this.show(res.message);
      }

    })

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  //做按钮点击
  shareClick:function(){
    // this.setData({
    //   showShareView: true,
    // })
  },

 //右边按钮
  submitClick:function(e){
    var formId = e.detail.formId;
    console.log('formId1=', e, formId);
    App.globalData.formId = formId;
    wx.navigateTo({
      url: './sureYuding?shopId=' + this.shopId,
    })

  },
  //去首页
  toIndexPage:function(){
    wx.switchTab({
      url: '../index/index',
    })

  },

  modeTap:function(){
    this.setData({
      showShareView: false,
      showTedianView: false,  
    })
  },

  tedianItemClick:function(e){
    var item = e.currentTarget.dataset.item
    this.setData({
      selectModel:item,
      showTedianView: true, 
    })
  },
  //中文
  zhongwenClick:function(){
    this.setData({
      showTedianView: false,
    })
    this.data.selectModel.shopName = this.data.detailModel.shopName
    wx.navigateTo({
      url: '../mine/fanyi?model='+JSON.stringify(this.data.selectModel),
    })

  },
  //保存
  baocunClick:function(){
    if (this.data.selectModel.customerDiscountId!=null){
      this.deleteYouhui();
    }else{
      this.addYouhui();
    } 
  },

  addYouhui:function(){
    var url = config.service.host + '/wechat/discount/'+this.data.selectModel.id+'/add.do';
    var params = {
      discountId:this.data.selectModel.id
    }
    net.doRequest(url,params,'POST',true,net.Token,res=>{
      if(res.code == 0){
        this.show('保存成功');
        this.requestDetailData()
        this.setData({
          showTedianView:false
        })
      }
    })
  },

  deleteYouhui:function(){
    var url = config.service.host + '/wechat/discount/' + this.data.selectModel.customerDiscountId+'/delete.do';
    var params = {
      customerDiscountId: this.data.selectModel.customerDiscountId

    }

    net.doRequest(url, params, 'DELETE', true, net.Token, res => {
      if (res.code == 0) {
        this.show('删除成功');
        this.requestDetailData()
        this.setData({
          showTedianView: false
        })

      }
    })

  },

  //打电话功能
  callClick:function(){
    wx.makePhoneCall({
      phoneNumber: this.data.detailModel.phone,
    })

  },

  toMap:function(){
    wx.openLocation({
      latitude: this.data.detailModel.latitude,
      longitude: this.data.detailModel.longitude,
      name: this.data.detailModel.shopName,
      address:this.data.detailModel.address
    })
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return{
      title:this.data.detailModel.shopName,
      path: '/pages/index/indexDetail?shopId='+ this.data.detailModel.id + ' & shopName=' + this.data.detailModel.shopName
      
    }
  },


})